package com.application;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.imessage.filter.TokenAuthorFilter;
import com.imessage.utils.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Configuration
@ComponentScan(basePackageClasses = MainApplication.class, useDefaultFilters = true)
public class WebAppConfigurate extends WebMvcConfigurationSupport {

    private final String adminU = "/admin/";

    private final String adminV = "admin/";
    @Autowired
    protected Environment environment;

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //    //定义时间格式转换器
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        converter.setObjectMapper(mapper);
        return converter;
    }

    //
//    //添加转换器
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //将我们定义的时间格式转换器添加到转换器列表中,
        //这样jackson格式化时候但凡遇到Date类型就会转换成我们定义的格式
        converters.add(jackson2HttpMessageConverter());
    }

    private void setAdminController(ViewControllerRegistry registry) {
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController(adminU + "main.html").setViewName(adminV + "main");

        registry.addViewController(adminU + "index.html").setViewName(adminV + "index");

        registry.addViewController(adminU + "comm/imp.html").setViewName(adminV + "comm/imp");

        registry.addViewController(adminU + "manage/menu/list.html").setViewName(adminV + "manage/menu/list");
        registry.addViewController(adminU + "manage/menu/modify.html").setViewName(adminV + "manage/menu/modify");
        registry.addViewController(adminU + "manage/menu/add.html").setViewName(adminV + "manage/menu/add");
        registry.addViewController(adminU + "manage/menu/form.html").setViewName(adminV + "manage/menu/form");

        registry.addViewController(adminU + "manage/role/list.html").setViewName(adminV + "manage/role/list");
        registry.addViewController(adminU + "manage/role/modify.html").setViewName(adminV + "manage/role/modify");
        registry.addViewController(adminU + "manage/role/add.html").setViewName(adminV + "manage/role/add");
        registry.addViewController(adminU + "manage/role/form.html").setViewName(adminV + "manage/role/form");

        registry.addViewController(adminU + "manage/user/list.html").setViewName(adminV + "manage/user/list");
        registry.addViewController(adminU + "manage/user/modify.html").setViewName(adminV + "manage/user/modify");
        registry.addViewController(adminU + "manage/user/add.html").setViewName(adminV + "manage/user/add");
        registry.addViewController(adminU + "manage/user/form.html").setViewName(adminV + "manage/user/form");

        registry.addViewController(adminU + "base/config/list.html").setViewName(adminV + "base/config/list");
        registry.addViewController(adminU + "base/config/modify.html").setViewName(adminV + "base/config/modify");
        registry.addViewController(adminU + "base/config/add.html").setViewName(adminV + "base/config/add");
        registry.addViewController(adminU + "base/config/form.html").setViewName(adminV + "base/config/form");

        registry.addViewController(adminU + "business/wuma/list.html").setViewName(adminV + "business/wuma/list");
        registry.addViewController(adminU + "business/wuma/modify.html").setViewName(adminV + "business/wuma/modify");
        registry.addViewController(adminU + "business/wuma/add.html").setViewName(adminV + "business/wuma/add");
        registry.addViewController(adminU + "business/wuma/form.html").setViewName(adminV + "business/wuma/form");

        //region 设备管理
        registry.addViewController(adminU + "device/client/list.html").setViewName(adminV + "device/client/list");
        registry.addViewController(adminU + "device/client/modify.html").setViewName(adminV + "device/client/modify");
        registry.addViewController(adminU + "device/client/add.html").setViewName(adminV + "device/client/add");
        //endregion


        //region 虚拟机管理
        registry.addViewController(adminU + "device/virtual/list.html").setViewName(adminV + "device/virtual/list");
        registry.addViewController(adminU + "device/virtual/modify.html").setViewName(adminV + "device/virtual/modify");
        registry.addViewController(adminU + "device/virtual/add.html").setViewName(adminV + "device/virtual/add");
        //endregion

        //region 产品管理
        registry.addViewController(adminU + "base/product/list.html").setViewName(adminV + "base/product/list");
        registry.addViewController(adminU + "base/product/modify.html").setViewName(adminV + "base/product/modify");
        registry.addViewController(adminU + "base/product/add.html").setViewName(adminV + "base/product/add");
        //endregion


        //region 苹果账号管理
        registry.addViewController(adminU + "business/apple/list.html").setViewName(adminV + "business/apple/list");
        registry.addViewController(adminU + "business/apple/modify.html").setViewName(adminV + "business/apple/modify");
        registry.addViewController(adminU + "business/apple/add.html").setViewName(adminV + "business/apple/add");
        //endregion

        //region 号码分组
        registry.addViewController(adminU + "no/group/list.html").setViewName(adminV + "no/group/list");
        registry.addViewController(adminU + "no/group/modify.html").setViewName(adminV + "no/group/modify");
        registry.addViewController(adminU + "no/group/add.html").setViewName(adminV + "no/group/add");
        //endregion

        //region 其它任务
        registry.addViewController(adminU + "business/othertask/list.html").setViewName(adminV + "business/othertask/list");
        registry.addViewController(adminU + "business/othertask/modify.html").setViewName(adminV + "business/othertask/modify");
        registry.addViewController(adminU + "business/othertask/add.html").setViewName(adminV + "business/othertask/add");
        registry.addViewController(adminU + "business/othertask/form.html").setViewName(adminV + "business/othertask/form");
        //endregion

        //region 发送任务
        registry.addViewController(adminU + "business/sendtask/list.html").setViewName(adminV + "business/sendtask/list");
        registry.addViewController(adminU + "business/sendtask/modify.html").setViewName(adminV + "business/sendtask/modify");
        registry.addViewController(adminU + "business/sendtask/add.html").setViewName(adminV + "business/sendtask/add");
        registry.addViewController(adminU + "business/sendtask/form.html").setViewName(adminV + "business/sendtask/form");
        registry.addViewController(adminU + "business/sendtask/detail.html").setViewName(adminV + "business/sendtask/detail");
        //endregion

        //region 正常号码
        registry.addViewController(adminU + "no/usephone/list.html").setViewName(adminV + "no/usephone/list");
        registry.addViewController(adminU + "no/usephone/modify.html").setViewName(adminV + "no/usephone/modify");
        registry.addViewController(adminU + "no/usephone/add.html").setViewName(adminV + "no/usephone/add");
        registry.addViewController(adminU + "no/usephone/form.html").setViewName(adminV + "no/usephone/form");
        //endregion


        //region 异常号码
        registry.addViewController(adminU + "no/errorphone/list.html").setViewName(adminV + "no/errorphone/list");
        registry.addViewController(adminU + "no/errorphone/modify.html").setViewName(adminV + "no/errorphone/modify");
        registry.addViewController(adminU + "no/errorphone/add.html").setViewName(adminV + "no/errorphone/add");
        registry.addViewController(adminU + "no/errorphone/form.html").setViewName(adminV + "no/errorphone/form");
        //endregion


    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        setAdminController(registry);
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        String relocHeader = "/img";
        String reLoc = String.format("file:///%s%s/", StrUtil.getOsResourceLoc(environment), relocHeader);
        registry.addResourceHandler(relocHeader + "/**").addResourceLocations(reLoc);

        super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePathPatternList = new ArrayList<String>();
        excludePathPatternList.add("/static/**");
        excludePathPatternList.add("/api/**");
        excludePathPatternList.add("/admin/index");
        registry.addInterceptor(new TokenAuthorFilter())
                .addPathPatterns("/admin/*/**")
                .addPathPatterns("/user/*/**").excludePathPatterns(excludePathPatternList);
    }
}
